package com.ds.home.service.impl;

import com.ds.config.ErrorListResultModel;
import com.ds.config.ErrorResultModel;
import com.ds.config.ListResultModel;
import com.ds.config.ResultModel;
import com.ds.context.JDSActionContext;
import com.ds.esb.config.EsbBeanAnnotation;
import com.ds.home.engine.AppEngine;
import com.ds.iot.*;
import com.ds.iot.api.inner.HomeService;
import com.ds.jds.core.User;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

@EsbBeanAnnotation(id = "HomeService", name = "HomeService", expressionArr = "HomeServiceImpl()", desc = "HomeService")

public class HomeServiceImpl implements HomeService {


    private final AppEngine appEngine;

    public HomeServiceImpl() {

        this.appEngine = AppEngine.getEngine(JDSActionContext.getActionContext().getSystemCode());
    }

    @Override
    public ResultModel<Place> createPlace(String name, String parentPlaceId,String personId) {
        ResultModel<Place> result = new ResultModel<Place>();
        try {
            Place place = appEngine.createPlace(name, parentPlaceId,personId);
            result.setData(place);
        } catch (HomeException e) {
            result = new ErrorResultModel();
            ((ErrorResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorResultModel) result).setErrdes(e.getMessage());
        }
        return result;

    }

    @Override
    public ResultModel<Boolean> savePlace(Place place) {
        ResultModel<Boolean> result = new ResultModel<Boolean>();

        try {
            appEngine.updatePlace(place);

        } catch (HomeException e) {
            result = new ErrorResultModel();
            ((ErrorResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ResultModel<Boolean> deletePlace(String placeId) {
        ResultModel<Boolean> result = new ResultModel<Boolean>();

        try {
            appEngine.deletePlace(placeId);

        } catch (HomeException e) {
            result = new ErrorResultModel();
            ((ErrorResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }


    @Override
    public ListResultModel<List<Place>> loadPlaceList(String[] placeIdList) {

        ListResultModel<List<Place>> result = new ListResultModel<List<Place>>();
        List<Place> ids = new ArrayList<Place>();
        try {

            for (String placeId : placeIdList) {
                ids.add(appEngine.getPlaceById(placeId));
            }
            result.setData(ids);
        } catch (HomeException e) {
            result = new ErrorListResultModel();
            ((ErrorListResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorListResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ListResultModel<Set<String>> getPlaceIds() {

        ListResultModel<Set<String>> result = new ListResultModel<Set<String>>();
        Set<String> ids = new LinkedHashSet<>();
        try {
            List<Place> places = appEngine.findAllUserPlace();
            for (Place place : places) {
                ids.add(place.getPlaceid());
            }
            result.setData(ids);
        } catch (HomeException e) {
            result = new ErrorListResultModel();
            ((ErrorListResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorListResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }


    @Override
    public ResultModel<Place> getPlaceById(String placeId) {
        ResultModel<Place> result = new ResultModel<Place>();
        try {
            Place place = appEngine.getPlaceById(placeId);
            result.setData(place);
        } catch (HomeException e) {
            result = new ErrorResultModel();
            ((ErrorResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }


    @Override
    public ResultModel<Boolean> addAlarm(Alarm alarm) {
        ResultModel<Boolean> result = new ResultModel<Boolean>();
        try {
            appEngine.updateAlarm(alarm);
        } catch (HomeException e) {
            result = new ErrorResultModel();
            ((ErrorResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorResultModel) result).setErrdes(e.getMessage());
        }
        return result;

    }

    @Override
    public ResultModel<Boolean> deleteAlarm(String alarmId) {
        ResultModel<Boolean> result = new ResultModel<Boolean>();
        try {
            appEngine.deleteAlarm(alarmId);
        } catch (HomeException e) {
            result = new ErrorResultModel();
            ((ErrorResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ResultModel<Alarm> getAlarm(String alarmId) {
        ResultModel<Alarm> result = new ResultModel<Alarm>();
        try {
            Alarm alarm = appEngine.getAlarmById(alarmId);
            result.setData(alarm);
        } catch (HomeException e) {
            result = new ErrorResultModel();
            ((ErrorResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ListResultModel<Set<String>> getAllAlarmsBySensorId(String sensorId) {
        ListResultModel<Set<String>> result = new ListResultModel<Set<String>>();
        Set<String> ids = new LinkedHashSet<>();
        try {
            List<Alarm> alarms = appEngine.getAlarmBySensorId(sensorId);
            for (Alarm alarm : alarms) {
                ids.add(alarm.getAlarmid());
            }
            result.setData(ids);
        } catch (HomeException e) {
            result = new ErrorListResultModel();
            ((ErrorListResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorListResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ResultModel<Area> createArea(String name, String placeId) {
        ResultModel<Area> result = new ResultModel<Area>();
        try {
            Area area = appEngine.createArea(name, placeId);

            result.setData(area);
        } catch (HomeException e) {
            result = new ErrorResultModel();
            ((ErrorResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ResultModel<Boolean> updateArea(Area area) {
        ResultModel<Boolean> result = new ResultModel<Boolean>();
        try {
            appEngine.updateArea(area);

        } catch (HomeException e) {
            result = new ErrorResultModel();
            ((ErrorResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ResultModel<Boolean> deleteArea(String areaId) {
        ResultModel<Boolean> result = new ResultModel<Boolean>();
        try {
            appEngine.deleteArea(areaId);

        } catch (HomeException e) {
            result = new ErrorResultModel();
            ((ErrorResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ListResultModel<List<Area>> getAreas(String[] areaIds) {

        ListResultModel<List<Area>> result = new ListResultModel<List<Area>>();
        List<Area> areas = new ArrayList<Area>();
        try {
            for (String areaId : areaIds) {
                areas.add(appEngine.getAreaById(areaId));
            }
            result.setData(areas);
        } catch (HomeException e) {
            result = new ErrorListResultModel();
            ((ErrorListResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorListResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ListResultModel<Set<String>> getAreaIdsbyPlaceId(String placeId) {
        ListResultModel<Set<String>> result = new ListResultModel<Set<String>>();

        try {

            result.setData(appEngine.getPlaceById(placeId).getAreaIds());
        } catch (HomeException e) {
            result = new ErrorListResultModel();
            ((ErrorListResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorListResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }


    @Override
    public ResultModel<Boolean> bindingArea(String sessionieee, String areaId) {
        ResultModel<Boolean> result = new ResultModel<Boolean>();
        try {
            appEngine.bindingArea(sessionieee,areaId);

        } catch (HomeException e) {
            result = new ErrorResultModel();
            ((ErrorResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ResultModel<Boolean> bindingPlace(String gwieee, String placeId) {
        ResultModel<Boolean> result = new ResultModel<Boolean>();
        try {
            appEngine.bindingPlace(gwieee,placeId);

        } catch (HomeException e) {
            result = new ErrorResultModel();
            ((ErrorResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }



    @Override
    public ResultModel<Alarm> creatAlarm(String sensorId) {
        ResultModel<Alarm> result = new ResultModel<Alarm>();
        try {
            Alarm alarm=    appEngine.creatAlarm(sensorId);
            result.setData(alarm);
        } catch (HomeException e) {
            result = new ErrorResultModel();
            ((ErrorResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ResultModel<Boolean> updateAlarm(Alarm alarm) {
        ResultModel<Boolean> result = new ResultModel<Boolean>();
        try {
            appEngine.updateAlarm(alarm);

        } catch (HomeException e) {
            result = new ErrorResultModel();
            ((ErrorResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ListResultModel<Set<String>> getAllUserByDeviceId(String deviceid) {
        ListResultModel<Set<String>> result = new ListResultModel<Set<String>>();
        Set<String> ids=new LinkedHashSet<>();
        try {
            List<User> users=appEngine.getAllUserByDeviceId(deviceid);
            for(User  user:users){
                ids.add(user.getId());
            }
            result.setData(ids);
        } catch (HomeException e) {
            result = new ErrorListResultModel();
            ((ErrorListResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorListResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ResultModel<Scene> getSceneById(String sensorSceneId) {
        ResultModel<Scene> result = new ResultModel<Scene>();
        try {
            Scene scene=    appEngine.getSceneById(sensorSceneId);
            result.setData(scene);
        } catch (HomeException e) {
            result = new ErrorResultModel();
            ((ErrorResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ResultModel<Boolean> deleteScene(String sensorSceneId) {
        ResultModel<Boolean> result = new ResultModel<Boolean>();
        try {
            appEngine.deleteScene(sensorSceneId);

        } catch (HomeException e) {
            result = new ErrorResultModel();
            ((ErrorResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ResultModel<Boolean> updateScene(Scene scene) {
        ResultModel<Boolean> result = new ResultModel<Boolean>();
        try {
            appEngine.updateScene(scene);

        } catch (HomeException e) {
            result = new ErrorResultModel();
            ((ErrorResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ResultModel<String> getMainUserByDeviceId(String serialno) {
        ResultModel<String> result = new ResultModel<String>();
        try {
            result.setData(   appEngine.getMainUserByDeviceId(serialno).getId());

        } catch (HomeException e) {
            result = new ErrorResultModel();
            ((ErrorResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ListResultModel<Set<String>> getAllAreaByPlaceId(String placeId) {
        ListResultModel<Set<String>> result = new ListResultModel<Set<String>>();
        Set<String> ids=new LinkedHashSet<>();
        try {
            List<Area> areas=appEngine.getAllAreaByPlaceId(placeId);
            for(Area  area:areas){
                ids.add(area.getAreaid());
            }
            result.setData(ids);
        } catch (HomeException e) {
            result = new ErrorListResultModel();
            ((ErrorListResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorListResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ListResultModel<List<Area>> loadAreaList(String[] strings) {
        ListResultModel<List<Area>> result = new ListResultModel<List<Area>>();
        List<Area> areas=new ArrayList<Area>();
        try {

            for(String  areaId:strings){
                areas.add(this.appEngine.getAreaById(areaId));
            }
            result.setData(areas);
        } catch (HomeException e) {
            result = new ErrorListResultModel();
            ((ErrorListResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorListResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ListResultModel<List<Alarm>> loadAlarmList(String[] strings) {
        ListResultModel<List<Alarm>> result = new ListResultModel<List<Alarm>>();
        List<Alarm> alarms=new ArrayList<Alarm>();
        try {

            for(String  alarmId:strings){
                alarms.add(this.appEngine.getAlarmById(alarmId));
            }
            result.setData(alarms);
        } catch (HomeException e) {
            result = new ErrorListResultModel<>();
            ((ErrorListResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorListResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ListResultModel<Set<String>> getAllPlaceByUserId(String userID) {
        ListResultModel<Set<String>> result = new ListResultModel<Set<String>>();
        Set<String> ids=new LinkedHashSet<>();
        try {
            List<Place> places=appEngine.getAllPlaceByUserId(userID);
            for(Place  place:places){
                ids.add(place.getPlaceid());
            }
            result.setData(ids);
        } catch (HomeException e) {
            result = new ErrorListResultModel();
            ((ErrorListResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorListResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ListResultModel<Set<String>> getSceneBySensorId(String sensorId) {
        ListResultModel<Set<String>> result = new ListResultModel<Set<String>>();
        Set<String> ids=new LinkedHashSet<>();
        try {
            List<Scene> scenes=appEngine.getSceneBySensorId(sensorId);
            for(Scene  scene:scenes){
                ids.add(scene.getSceneid());
            }
            result.setData(ids);
        } catch (HomeException e) {
            result = new ErrorListResultModel();
            ((ErrorListResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorListResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ListResultModel<List<Sensortype>> getSensorTypesByGatewayId(String gatewayId) {
        ListResultModel<List<Sensortype>> result = new ListResultModel<List<Sensortype>>();
        List<String> ids=new ArrayList<String>();
        try {
            List<Sensortype> sensortype=appEngine.getSensorTypesByGatewayId(gatewayId);
            result.setData(sensortype);
        } catch (HomeException e) {
            result = new ErrorListResultModel();
            ((ErrorListResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorListResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ResultModel<Boolean> deleteGateway(String deviceid) {
        ResultModel<Boolean> result = new ResultModel<Boolean>();
        try {
            appEngine.deleteZNode(deviceid);

        } catch (HomeException e) {
            result = new ErrorResultModel();
            ((ErrorResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ResultModel<Area> getAreaById(String areaId) {
        ResultModel<Area> result = new ResultModel<Area>();
        try {
            Area area= appEngine.getAreaById(areaId);
            result.setData(area);
        } catch (HomeException e) {
            result = new ErrorResultModel();
            ((ErrorResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ListResultModel<Set<String>> getAreasByPlaceId(String placeId) {
        ListResultModel<Set<String>> result = new ListResultModel<Set<String>>();
        try {
            Set<String> areaIds= appEngine.getPlaceById(placeId).getAreaIds();
            result.setData(areaIds);
        } catch (HomeException e) {
            result = new ErrorListResultModel();
            ((ErrorListResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorListResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ListResultModel<Set<String>> getAllPlaceIds() {
        ListResultModel<Set<String>> result = new ListResultModel<Set<String>>();
        try {
            List<Place> places= appEngine.findAllUserPlace();
            Set<String> ids=new LinkedHashSet<>();
            for(Place place:places){
                ids.add(place.getPlaceid());
            }
            result.setData(ids);
        } catch (HomeException e) {
            result = new ErrorListResultModel();
            ((ErrorListResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorListResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }
}
